package com.academic.system.service.impl;

import com.academic.common.core.domain.entity.SysCourse;
import com.academic.common.core.domain.entity.UserCourse;
import com.academic.common.core.page.TableDataInfo;
import com.academic.system.domain.rep.CourseRep;
import com.academic.system.domain.req.NewReq;
import com.academic.system.mapper.SysUsersMapper;
import com.academic.system.mapper.dao.SysCourseDao;
import com.academic.system.mapper.dao.UserCourseDao;
import com.academic.system.service.SysCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysCourse)表服务实现类
 *
 * @author makejava
 * @since 2024-10-31 22:55:00
 */
@Service("sysCourseService")
public class SysCourseServiceImpl extends ServiceImpl<SysCourseDao, SysCourse> implements SysCourseService {
    private final SysCourseDao sysCourseDao;
    private final UserCourseDao userCourseDao;
    private final SysBookServiceImpl sysBookService;


    public SysCourseServiceImpl(SysCourseDao sysCourseDao, UserCourseDao userCourseDao, SysUsersMapper usersMapper, SysBookServiceImpl sysBookService) {
        this.sysCourseDao = sysCourseDao;
        this.userCourseDao = userCourseDao;
        this.sysBookService = sysBookService;
    }

    @Override
    public TableDataInfo getCourseHistory(NewReq newReq) {
        PageHelper.startPage(newReq.getPageNum(), newReq.getPageSize());
        List<CourseRep> reps = new ArrayList<>();
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", newReq.getId());
        List<UserCourse> userCourses = userCourseDao.selectList(queryWrapper);
        PageInfo<UserCourse> pageInfo = new PageInfo<>(userCourses);
        List<Long> courseIds = userCourses
                .stream()
                .map(UserCourse::getCourseId)
                .collect(Collectors.toList());
        QueryWrapper<SysCourse> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id", courseIds);
        //此处已经获取到当前用户的所有课程信息
        List<SysCourse> sysCourses = sysCourseDao.selectList(queryWrapper1);
        for (UserCourse userCourse : userCourses) {
            CourseRep courseRep = new CourseRep();
            SysCourse sysCourse = sysCourses.stream()
                    .filter(sysCourse1 -> userCourse.getCourseId()
                            .equals(sysCourse1.getId()))
                    .findFirst()
                    .get();
            courseRep.setCourseId(sysCourse.getId());
            courseRep.setCourseName(sysCourse.getCourseName());
            courseRep.setReadExam(sysCourse.getReadExam());
            courseRep.setClassroom(userCourse.getClassroom());
            courseRep.setReadYears(sysCourse.getReadYears());
            courseRep.setTeacherName(sysCourse.getTeacherName());
            courseRep.setTeacherPhone(sysCourse.getTeacherPhone());
            courseRep.setCourseCount(sysCourse.getCourseCount());
            courseRep.setTeachingWeek(sysCourse.getTeachingWeek());
            reps.add(courseRep);
        }
        return sysBookService.getDataTable(reps,pageInfo.getTotal());
    }
}

